package com.sumainfor.common.easypoiExcel.utils;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.export.ExcelExportService;
import lombok.extern.log4j.Log4j2;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:
 * Excel多sheet导出工具类
 * @Auther: zhlu
 * @Date: 2019/6/17 11:08
 * @Description:
 */
@Log4j2
public class ExcelUtils {

    /**
     * 一个excel 创建多个sheet
     *
     * @param list
     *            多个Map key title 对应表格Title
     *            key entity 对应表格对应实体
     *            key data 对应Collection 数据
     *            key excelType  对应 0.实体类   1.map
     * @return
     */
    public static Workbook exportExcel(List<Map<String, Object>> list, ExcelType type) {
        Workbook workbook;
        if(ExcelType.HSSF.equals(type)){
            workbook=new HSSFWorkbook();
        }else{
            workbook=new XSSFWorkbook();
        }
        for (Map<String, Object> map : list) {
            Integer excelType=Integer.valueOf(map.get("excelType").toString());

            ExcelExportService server = new ExcelExportService();
            ExportParams params=(ExportParams)map.get("params");
            Collection<?> data=(Collection<?>)map.get("data");
            if(excelType==0){
                Class<?> entity=(Class<?>)map.get("entity");
                server.createSheet(workbook,params,entity,data);
            }else if(excelType==1){
                List<ExcelExportEntity> entityList=(List<ExcelExportEntity>)map.get("EntityList");
                server.createSheetForMap(workbook,params,entityList,data);
            }
        }
        return workbook;
    }

}
